package com.pipi.jpa.controller;

import com.pipi.jpa.model.Book;
import com.pipi.jpa.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

/**
 * Created by pipi on 2017/9/1.
 */
@Controller
@RequestMapping(path="/book")
public class BookController {

    @Autowired
    private BookRepository repository;

    @GetMapping("/add")
    @ResponseBody
    public String add(){

        int max=10;
        int min=1;
        Random random = new Random();
        for(int i=0;i<20;i++){
            Book book = new Book();
            book.setName("苏东坡" + i);
            book.setUserId(random.nextInt(max)%10+min);
            repository.save(book);
        }
        return "saved";
    }

    @GetMapping("/list")
    @ResponseBody
    public Iterable<Book>  listAll(){
        return repository.findAll();
    }
}
